#include "engine.h"
#include "introstate.h"
#include <iostream>
#include <sstream>

using namespace std;

int main(){
	CEngine engine;

	engine.Init("Engine Test v0.2b", 800, 600, 32, 100, false, true);
	engine.ChangeState(CIntroState::Instance());

	sf::Clock clockFrequency;
	sf::Clock clockFrame;
    const float FREQ_TICK_STEP = (1.0f / 100);
	const float FRAME_TICK_STEP = (1.0f / 60);
	float sleepTime;
	float tick, tick2;

	while(engine.Running() && engine.window.IsOpened()) {
		if(clockFrequency.GetElapsedTime() > FREQ_TICK_STEP) {
			engine.HandleEvents();
			engine.Update();

			engine.setCurFrequence(int(1 / clockFrequency.GetElapsedTime()));
			clockFrequency.Reset();
		} else {
			sleepTime = ((tick = (FREQ_TICK_STEP - clockFrequency.GetElapsedTime())) < (tick2 = (FRAME_TICK_STEP - clockFrame.GetElapsedTime()))) ? tick*1000 : tick2*1000;
			if(sleepTime > 0)
				Sleep(sleepTime);
		}

		if(clockFrame.GetElapsedTime() > FRAME_TICK_STEP) {
			engine.Draw();

			engine.setCurFramerate(int(1 / clockFrame.GetElapsedTime()));
			clockFrame.Reset();
		}
	}

	engine.Cleanup();

	return 0;
}
